package com.emm.yixun.customer.service.impl.app.manage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.FollowService;
import com.emm.yixun.basic.service.ImageService;
import com.emm.yixun.common.app.GetFollowListReqDto;
import com.emm.yixun.common.app.GetFollowListResDto;
import com.emm.yixun.common.app.servletentity.ContentListRes;
import com.emm.yixun.common.app.servletentity.FollowListRes;
import com.emm.yixun.common.model.Follow;
import com.emm.yixun.common.model.Image;
import com.emm.yixun.common.model.dto.FollowDto;
import com.emm.yixun.common.model.dto.ImageDto;
import com.emm.yixun.common.model.enums.FollowModeEnum;
import com.emm.yixun.common.model.enums.FollowTypeEnum;

@Service("getFollowListManage")
public class GetFollowListManage {

	@Autowired
	private FollowService followService;
	@Autowired
	private ImageService imageService;
	
	/**
	 * 查询客户跟进列表
	 * @param reqDto
	 * @return
	 */
	public GetFollowListResDto getFollowList(GetFollowListReqDto reqDto) {
		GetFollowListResDto resDto = new GetFollowListResDto();
		try{
			//1、查询客户跟进
			FollowDto followDto = new FollowDto();
			followDto.setMerchantId(reqDto.getMerchantId());
			followDto.setProjectCode(reqDto.getProjectCode());
			followDto.setCustomerId(Long.parseLong(reqDto.getCustomerId()));
			followDto.setOrders("create_time desc");
			List<Follow> getList = followService.selectListByDto(followDto);
			List<FollowListRes> followList = new ArrayList<FollowListRes>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			//遍历跟进列表
			for(Follow follow: getList){
				FollowListRes flRes = new FollowListRes();
				if(StringUtils.isNotEmpty(follow.getFollowMode())){
					flRes.setFollowMode(FollowModeEnum.getByNo(follow.getFollowMode()).getDes());
				}
				if(follow.getFollowTime() != null){
					flRes.setFollowTime(sdf.format(follow.getFollowTime()));
				}
				flRes.setVoiceTime(follow.getVoiceTime());
				flRes.setVoiceLocalAddr(follow.getVoiceLocalAddr());
				if(StringUtils.isNotEmpty(follow.getFollowType())){
					flRes.setFollowType(follow.getFollowType());
				}
				//通话记录ID
				if(StringUtils.isNotEmpty(String.valueOf(follow.getTelRecordId()))){
					flRes.setTelRecordId(String.valueOf(follow.getTelRecordId()));
				}else{
					flRes.setTelRecordId(null);
				}
				//组装跟进内容---start
				List<ContentListRes> contentList = new ArrayList<ContentListRes>();
				ContentListRes clRes = new ContentListRes();
				if(FollowTypeEnum.TEXT.getNo().equals(follow.getFollowType())){
					//文字
					clRes = new ContentListRes();
					clRes.setFollowContent(follow.getFollowContent());
					contentList.add(clRes);
				}else{
					//图片及语音,从上传表中获取
					ImageDto imageDto = new ImageDto();
					imageDto.setObjectId(follow.getId());
					List<Image> imageList = imageService.selectListByDto(imageDto);
					for(Image image: imageList){
						clRes = new ContentListRes();
						clRes.setFollowContent(image.getSmallUrl());
						contentList.add(clRes);
					}
				}
				flRes.setContentList(contentList);
				//组装跟进内容---end
				followList.add(flRes);
			}
			resDto.setFollowList(followList);
			resDto.setResult("1");
		}catch(Exception e){
			e.printStackTrace();
			resDto.setErrorCode("00000");
			resDto.setErrorMsg("查询跟进失败!");
			resDto.setResult("0");
			return resDto;
		}
		return resDto;
	}
}
